﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class CTKTDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public int CheckInvoiceLock(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoiceLockedKT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[3].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int CheckInvoicePost(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder, object pi_Posted)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Check", null) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoicePostedKT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[4].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int CheckInvoiceStatus(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoiceStatusKT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[3].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public object Cong_So_Ct(string ps_Subsidiary, string ps_InvoiceCode, object ps_BookNum, object pd_Ngay_Ct, object ps_So_Ct)
        {
            object obj2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ma_Ct", ps_InvoiceCode), new SqlParameter("p_BookNum", ps_BookNum), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_So_Ct", ps_So_Ct) };
            values[4].Direction = ParameterDirection.InputOutput;
            values[4].SqlDbType = SqlDbType.NVarChar;
            values[4].Size = 20;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "Cong_So_CtK";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                obj2 = values[4].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return obj2;
        }

        public bool Delete(string ps_UserName, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", po_KeyCode), new SqlParameter("p_UserName", ps_UserName), new SqlParameter() };
            values[2].Direction = ParameterDirection.ReturnValue;
            values[2].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[2].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public bool DeleteDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK0_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataSet GetDetailDataSet(string ps_InvoiceOrder)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK0_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataRow GetMasterDataRow(string ps_InvoiceOrder)
        {
            DataRow row;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK_One_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                if (dataTable.Rows.Count == 0)
                {
                    dataTable.Rows.Add(dataTable.NewRow());
                    dataTable.AcceptChanges();
                }
                row = dataTable.Rows[0];
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return row;
        }

        public DataTable GetMasterTable(string ps_Subsidiary, int pi_DaysFilter, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_So_Ct1, object ps_So_Ct2, object ps_Ma_Ct, object ps_Ma_Tte, object ps_Tk, object ps_No_Co, object ps_Tk_Du, object ps_Ma_Dt, object pi_Tien1, object pi_Tien2, object ps_Ma_Km, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Sp, object ps_Dien_Giai, object ps_Dien_GiaiC, object ps_Ma_Loai1, object ps_Ma_Loai2, object ps_Ma_Loai3, int ii_FilterByUser, string ps_UserName, object ps_Notes, int pi_Posted, int pi_Locked)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Ma_DvCs", ps_Subsidiary), new SqlParameter("p_Ngay_Loc", pi_DaysFilter), new SqlParameter("p_Ngay1", pd_Ngay_Ct1), new SqlParameter("p_Ngay2", pd_Ngay_Ct2), new SqlParameter("p_So_Ct1", ps_So_Ct1), new SqlParameter("p_So_Ct2", ps_So_Ct2), new SqlParameter("p_Ma_Ct", ps_Ma_Ct), new SqlParameter("p_Ma_Tte", ps_Ma_Tte), new SqlParameter("p_Tk", ps_Tk), new SqlParameter("p_No_Co", ps_No_Co), new SqlParameter("p_Tk_Du", ps_Tk_Du), new SqlParameter("p_Ma_Dt", ps_Ma_Dt), new SqlParameter("p_Tien1", pi_Tien1), new SqlParameter("p_Tien2", pi_Tien2), new SqlParameter("p_Ma_Km", ps_Ma_Km), new SqlParameter("p_Ma_Bp", ps_Ma_Bp), 
                new SqlParameter("p_Ma_HD", ps_Ma_Hd), new SqlParameter("p_Ma_Sp", ps_Ma_Sp), new SqlParameter("p_Dien_Giai", ps_Dien_Giai), new SqlParameter("p_Dien_GiaiC", ps_Dien_GiaiC), new SqlParameter("p_Ma_Loai1", ps_Ma_Loai1), new SqlParameter("p_Ma_Loai2", ps_Ma_Loai2), new SqlParameter("p_Ma_Loai3", ps_Ma_Loai3), new SqlParameter("p_FilterByUser", ii_FilterByUser), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Notes", ps_Notes), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Locked", pi_Locked)
             };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public void Lock(string ps_UserName, object ps_InvoiceOrder, bool pb_Locked)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Locked", pb_Locked) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UpdateLockedKT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }

        public bool NumberExisted(string ps_Subsidiary, bool pb_CheckNewRow, string ps_InvoiceOrder, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_BookNum, object ps_So_Ct)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Moi_Sua", pb_CheckNewRow), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Ma_Ct", ps_InvoiceCode), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_BookNum", ps_BookNum), new SqlParameter("p_So_Ct", ps_So_Ct), new SqlParameter("p_Check", null) };
            values[7].Direction = ParameterDirection.Output;
            values[7].SqlDbType = SqlDbType.Bit;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckSoCtK";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = (bool) values[7].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public void Post(string ps_UserName, object ps_InvoiceOrder, object pi_Posted)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Posted", pi_Posted) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UpdatePostedKT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }

        public void PostSoCai(SqlConnection po_Connection, SqlTransaction po_Transaction, object ps_InvoiceOrder, object pi_Posted)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.CommandText = "CtKT_SoCaiCN_Save";
                command.ExecuteNonQuery();
                if (((int) pi_Posted) != 0)
                {
                    command.CommandText = "SoCaiTt_Calculate";
                    command.ExecuteNonQuery();
                }
                command.CommandText = "CtKT_SoCaiKT_Save";
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
        }

        public bool Save(bool pb_InvoiceHistory, bool pb_NewChecked, string ps_Subsidiary, string ps_UserName, DataRow po_MasterDataRow, DataTable po_DetailTable)
        {
            bool flag = false;
            foreach (DataRow row in po_DetailTable.Rows)
            {
                if (((row.RowState != DataRowState.Deleted) && (((decimal) row["Tien_Nt"]) != 0M)) && ((((decimal) row["Tien"]) != 0M) && !string.IsNullOrEmpty(row["Tk"].ToString())))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                this.Delete(ps_UserName, po_MasterDataRow["Stt"]);
                return false;
            }
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            if (!pb_NewChecked && pb_InvoiceHistory)
            {
                this.SaveHistory(SSConnection, transaction, ps_UserName, po_MasterDataRow["Stt"]);
            }
            flag = true;
            foreach (DataRow row2 in po_DetailTable.Rows)
            {
                row2.EndEdit();
                if (((row2.RowState != DataRowState.Deleted) && (((decimal) row2["Tien_Nt"]) != 0M)) && ((((decimal) row2["Tien"]) != 0M) && !string.IsNullOrEmpty(row2["Tk"].ToString())))
                {
                    if (((row2.RowState != DataRowState.Added) && (row2.RowState != DataRowState.Modified)) || (this.SaveDetailDataRow(SSConnection, transaction, po_MasterDataRow["Stt"], row2) == 1))
                    {
                        continue;
                    }
                    flag = false;
                    break;
                }
                if (row2.RowState == DataRowState.Deleted)
                {
                    this.DeleteDetailDataRow(SSConnection, transaction, row2["Stt0", DataRowVersion.Original]);
                }
                else
                {
                    this.DeleteDetailDataRow(SSConnection, transaction, row2["Stt0"]);
                }
            }
            if (flag && (this.SaveMasterDataRow(SSConnection, transaction, pb_NewChecked, ps_Subsidiary, po_MasterDataRow) == 1))
            {
                this.PostSoCai(SSConnection, transaction, po_MasterDataRow["Stt"], po_MasterDataRow["Posted"]);
                transaction.Commit();
                SSDataAccess.CloseConnection(SSConnection);
                po_DetailTable.AcceptChanges();
                po_MasterDataRow.Table.AcceptChanges();
                return true;
            }
            transaction.Rollback();
            SSDataAccess.CloseConnection(SSConnection);
            return false;
        }

        public int SaveDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Stt_NV", row["Stt_NV"]), new SqlParameter("p_No_Co", row["No_Co"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Ma_Tte", row["Ma_Tte"]), new SqlParameter("p_Ty_Gia", row["Ty_Gia"]), new SqlParameter("p_Ty_Gia_Ht", row["Ty_Gia_Ht"]), new SqlParameter("p_Tien_Nt", row["Tien_Nt"]), new SqlParameter("p_Tien", row["Tien"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Ma_Hd", row["Ma_Hd"]), new SqlParameter("p_Ma_Dt", row["Ma_Dt"]), new SqlParameter("p_Han_Tt", row["Han_Tt"]), new SqlParameter("p_Ma_Km", row["Ma_Km"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), 
                new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x12].Direction = ParameterDirection.Output;
            values[0x12].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK0_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[0x12].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        private void SaveHistory(SqlConnection po_Connection, SqlTransaction po_Transaction, string ps_UserName, object ps_InvoiceOrder)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Save_Delete", 1), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK_Log_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
        }

        public int SaveMasterDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, string ps_Subsidiary, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Nh_Ct", row["Nh_Ct"]), new SqlParameter("p_Ma_Ct", row["Ma_Ct"]), new SqlParameter("p_Ngay_Ps", row["Ngay_Ps"]), new SqlParameter("p_Ngay_Ct", row["Ngay_Ct"]), new SqlParameter("p_BookNum", row["BookNum"]), new SqlParameter("p_So_Ct", row["So_Ct"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Ma_Hd", row["Ma_Hd"]), new SqlParameter("p_Ma_Dt", row["Ma_Dt"]), new SqlParameter("p_Ong_Ba", row["Ong_Ba"]), new SqlParameter("p_Dia_Chi", row["Dia_Chi"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), 
                new SqlParameter("p_Ma_Tte", row["Ma_Tte"]), new SqlParameter("p_Ty_Gia", row["Ty_Gia"]), new SqlParameter("p_Ty_Gia_Ht", row["Ty_Gia_Ht"]), new SqlParameter("p_TTien_NtN", row["TTien_NtN"]), new SqlParameter("p_TTien_NtC", row["TTien_NtC"]), new SqlParameter("p_TTien", row["TTien"]), new SqlParameter("p_TTien_VAT0", row["TTien_VAT0"]), new SqlParameter("p_IL_TTien", row["IL_TTien"]), new SqlParameter("p_UN_TTien", row["UN_TTien"]), new SqlParameter("p_Ct_Di_Kem", row["Ct_Di_Kem"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_UserName", row["UserName"]), new SqlParameter("p_Posted", row["Posted"]), new SqlParameter("p_Locked", row["Locked"]), 
                new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x20].Direction = ParameterDirection.Output;
            values[0x20].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtK_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[0x20].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

